package com.ruoyi.web.runner;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 13596
 */
@Configuration
@Slf4j
public class ThreadPoolExecutorConfiguration {

	@Bean
	public ThreadPoolExecutor ethThreadPoolExecutor(){
		return new CustomThreadPoolExecutor(1,
				1,
				0L,
				TimeUnit.MILLISECONDS,
				new LinkedBlockingQueue<>(16),
				r -> {
					Thread thread = new Thread(r, "eth-" + r.hashCode());
					thread.setUncaughtExceptionHandler((t, e) -> {
						throw new RuntimeException(e.getMessage());
					});
					return thread;
				},
				new ThreadPoolExecutor.AbortPolicy()
		);
	}

	@Bean
	public ThreadPoolExecutor bscThreadPoolExecutor(){
		return new CustomThreadPoolExecutor(1,
				1,
				0L,
				TimeUnit.MILLISECONDS,
				new LinkedBlockingQueue<>(16),
				r -> {
					Thread thread = new Thread(r, "bsc-" + r.hashCode());
					thread.setUncaughtExceptionHandler((t, e) -> {
						throw new RuntimeException(e.getMessage());
					});
					return thread;
				},
				new ThreadPoolExecutor.AbortPolicy()
		);
	}

	@Bean
	public ThreadPoolExecutor tronThreadPoolExecutor(){
		return new CustomThreadPoolExecutor(1,
				1,
				0L,
				TimeUnit.MILLISECONDS,
				new LinkedBlockingQueue<>(16),
				r -> new Thread(r,"tron-"+r.hashCode()),
				new ThreadPoolExecutor.AbortPolicy()
		);
	}

	@Bean
	public ThreadPoolExecutor accountListThreadPoolExecutor(){
		return new ThreadPoolExecutor(10,
				20,
				10L,
				TimeUnit.SECONDS,
				new LinkedBlockingQueue<>(128),
				Executors.defaultThreadFactory(),
				new ThreadPoolExecutor.AbortPolicy()
		);
	}
}
